import Mock from 'mockjs';
import { Request, Response } from 'umi';
import { IGoodsType } from './goods';
import { IData } from '@/pages/main/right/sub1/type'

// 获取当前时间
const timeRandom = () => {
  let now = Date.now();
  // let y = now.getFullYear();
  // let mm = now.getMonth() + 1;
  // let d = now.getDate();
  // let h = now.getHours();
  // let minute = now.getMinutes();
  // let s = now.getSeconds();
  // return `${y}-${mm}-${d} ${h}:${minute}:${s}`;
  return now
}
// 商品种类
const goodsType: IGoodsType[] = [
  { id: 0, type: '水果' },
  { id: 1, type: '零食' },
  { id: 2, type: '主食' },
  { id: 3, type: '玩具' },
]

// 商品规格
const goodsNorms: IGoodsType[] = [
  { id: 0, type: '袋' },
  { id: 1, type: '根' },
  { id: 2, type: '盒' },
  { id: 3, type: '提' },
]

// 表格数据
const goodsData: IData[] = [
  {
    key: 1,
    goodsCode: '001',
    goodsName: '香蕉',
    norms: '根',
    goodsType: '水果',
    unit: 5.32,
    time: timeRandom()
  },
  {
    key: 2,
    goodsCode: '001',
    goodsName: '西瓜',
    norms: '个',
    goodsType: '水果',
    unit: 20.03,
    time: timeRandom()
  },
]




export default {
  // 请求商品规格
  'GET /getGoodsType': (req: Request, res: Response) => {
    res.send({
      data: goodsType,
      msg: '请求成功'
    })
  },
  // 请求商品类型
  'GET /getGoodsNorms': (req: Request, res: Response) => {
    res.send({
      data: goodsNorms,
      msg: '请求成功'
    })
  },
  // 请求商品表格数据
  'GET /getFormData': (req: Request, res: Response) => {
    res.send({
      data: goodsData,
      msg: '请求成功'
    })
  },
  // 新增商品
  'POST /addGoods': (req: Request, res: Response) => {
    let key = Date.now()
    let time = timeRandom()
    let addArr = Object.assign({}, req.body, { key, time })
    goodsData.push(addArr)

    res.send({
      mag: '请求成功',
      code: 200,
    })
  },
  // 删除商品
  'POST /delete': (req: Request, res: Response) => {
    goodsData.forEach((item, i) => {
      if (item.key === req.body.key) {
        goodsData.splice(i, 1)
      }
    })
    res.send({
      msg: '请求成功',
      code: 200
    })
  },
  // 搜索商品
  'GET /search': (req: Request, res: Response) => {

    console.log(req.query);
    
    const searchData = (): IData[] => {
      // 如果商品类型，商品名称和时间段都传进来了
      if(req.query.goodsType && req.query.goodsName && req.query.time) {
        console.log(111);
        
        let typeResult: IData[] = []
        let nameResult: IData[] = []
        let result: IData[] = []
        goodsData.forEach((item, i) => {
          if (item.goodsType === req.query.goodsType) {
            typeResult.push(item)
          }
        })
        typeResult.forEach((item, i) => {
          if (item.goodsName === req.query.goodsName) {
            nameResult.push(item)
          }
        })
        nameResult.forEach((item, i) => {
          if(item.time! >= req.query.time[0] && item.time! <= req.query.time[1]) {
            result.push(item)
          }
        })
        return result
      }
      // 如果只传入了商品类型
      if(req.query.goodsType && !req.query.goodsName && !req.query.time) {
        console.log(222);
        let typeResult: IData[] = []
        goodsData.forEach((item, i) => {
          if (item.goodsType === req.query.goodsType) {
            typeResult.push(item)
          }
        })
        return typeResult
      }
      // 如果只传入了商品名字
      if(req.query.goodsName && !req.query.goodsType && !req.query.time) {
        console.log(333);
        let nameResult: IData[] = []
        goodsData.forEach((item, i) => {
          if (item.goodsName === req.query.goodsName) {
            nameResult.push(item)
          }
        })
        return nameResult
      }
      // 如果只传入了时间段
      if(req.query.time && !req.query.goodsType && !req.query.goodsName) {
        console.log(444);
        let timeResult: IData[] = []
        goodsData.forEach((item, i) => {
          if(item.time! >= req.query.time[0] && item.time! <= req.query.time[1]) {
            timeResult.push(item)
          }
        })
        return timeResult
      }
      // 如果传入了商品类型和名字
      if(req.query.goodsType && req.query.goodsName && !req.query.time) {
        console.log(555);
        let typeResult: IData[] = []
        let nameResult: IData[] = []
        goodsData.forEach((item, i) => {
          if (item.goodsType === req.query.goodsType) {
            typeResult.push(item)
          }
        })
        typeResult.forEach((item, i) => {
          if (item.goodsName === req.query.goodsName) {
            nameResult.push(item)
          }
        })
        return nameResult
      }
      // 如果传入了商品类型和时间段
      if(req.query.goodsType && req.query.time && !req.query.goodsName) {
        console.log(666);
        let typeResult: IData[] = []
        let timeResult: IData[] = []
        goodsData.forEach((item, i) => {
          if (item.goodsType === req.query.goodsType) {
            typeResult.push(item)
          }
        })
        typeResult.forEach((item, i) => {
          if(item.time! >= req.query.time[0] && item.time! <= req.query.time[1]) {
            timeResult.push(item)
          }
        })
        return timeResult
      }
      // 如果传入了商品名字和时间段
      if(req.query.goodsName && req.query.time && !req.query.goodsType) {
        console.log(777);
        let nameResult: IData[] = []
        let timeResult: IData[] = []
        goodsData.forEach((item, i) => {
          if (item.goodsName === req.query.goodsName) {
            nameResult.push(item)
          }
        })
        nameResult.forEach((item, i) => {
          if(item.time! >= req.query.time[0] && item.time! <= req.query.time[1]) {
            timeResult.push(item)
          }
        })
        return timeResult
      }
      // 如果什么都没有，则直接返回原始值
      console.log(888);
      
      return goodsData
    }
    res.send({
      msg: '请求成功',
      data: searchData(),
      code: 200
    })
  },
  // 修改商品
  'POST /modify': (req: Request, res: Response) => {
    goodsData.forEach((item, i) => {
      if (item.key == req.body.selectKey) {
        item.goodsCode = req.body.goodsCode
        item.goodsName = req.body.goodsName
        item.norms = req.body.norms
        item.goodsType = req.body.goodsType
        item.unit = req.body.unit
      }
    })
    res.send({
      msg: '请求成功',
      code: 200
    })
  },
  // 重置时将数据返回
  'GET /reset':(req: Request, res: Response) => {
    res.send({
      msg: '请求成功',
      code: 200
    })
  }
}